import time

import allure
from selenium.webdriver.common.by import By
from .base import Base


class ProductListingPage(Base):     # 商品上架页
    __NUMBER = (By.CSS_SELECTOR, '.app-container form > div:nth-child(1) input.el-input__inner')    # 商品编号
    __NAME = (By.CSS_SELECTOR, '.app-container form > div:nth-child(2) input.el-input__inner')      # 商品名字
    __SELLING_PRICE = (By.CSS_SELECTOR, '.app-container form >div:nth-child(3) input.el-input__inner')  # 商品售价
    __IS_NO_NEW_PRODUCT = (By.CSS_SELECTOR, '.app-container form >div:nth-child(4) div.el-radio-group > label:nth-child(2)')    # 是否新品
    __IS_NO_HOT_SALE = (By.CSS_SELECTOR, '.app-container form >div:nth-child(5) div.el-radio-group > label:nth-child(2)')  # 是否热卖
    __IS_NO_FOR_SALE = (By.CSS_SELECTOR, '.app-container form >div:nth-child(6) div.el-radio-group > label:nth-child(2)')  # 是否在售
    __COMMODITY_IMG = (By.CSS_SELECTOR, 'div.el-upload.el-upload--text > input')    # 商品图片
    __COMMODITY_IMG_SUCCEED = (By.CSS_SELECTOR, '.el-upload.el-upload--text > img')  # 商品图片上传成功
    __PUBLICITY_GALLERY = (By.CSS_SELECTOR, '.el-upload.el-upload--picture-card > input')     # 宣传画廊
    __PUBLICITY_GALLERY_FAIL = (By.CSS_SELECTOR, ".el-message__content")     # 宣传画廊上传过多
    __PUT_AWAY_ERROR = (By.CLASS_NAME, "el-message-box__message")  # 上架报错
    __PUT_AWAY = (By.CSS_SELECTOR, '.op-container > button:nth-child(2)')   # 上架按钮

    def input_message(self, img_name, number=None, name=None, selling_price=None, new_product=None, hot_sale=None, for_sale=None, commodity_img=None, publicity_gallery=None):  # 输入信息
        with allure.step("信息输入"):
            if number:
                self.find_key(self.__NUMBER, number)
            if name:
                self.find_key(self.__NAME, name)
            if selling_price:
                self.find_key(self.__SELLING_PRICE, selling_price)
            if commodity_img:       # 商品图片
                file_path = self.file_path(commodity_img)
                self.find_key(self.__COMMODITY_IMG, file_path)
                self.wait(self.__COMMODITY_IMG_SUCCEED, 30)
            if publicity_gallery:   # 宣传画廊
                if type(publicity_gallery) == str:  # 单个图片
                    file_path = self.file_path(publicity_gallery)
                    self.find_key(self.__PUBLICITY_GALLERY, file_path)
                else:   # 多张图片
                    for item in publicity_gallery:
                        file_path = self.file_path(item)
                        self.find_key(self.__PUBLICITY_GALLERY, file_path)
                        self.implicitly_wait(0.3)
                        if self.find(self.__PUBLICITY_GALLERY_FAIL):      # 有错误弹窗时
                            self.wait(self.__PUBLICITY_GALLERY_FAIL)
                            text = self.find_text(self.__PUBLICITY_GALLERY_FAIL)
                            self.images_log(img_name, text)
                            return text
            self.implicitly_wait(10)
            if new_product:     # 是否新品
                if new_product == "非新品":
                    self.find_click(self.__IS_NO_NEW_PRODUCT)
            if hot_sale:     # 是否热卖
                if hot_sale == "热卖":
                    self.find_click(self.__IS_NO_HOT_SALE)
            if for_sale:     # 是否在售
                if for_sale == "未售":
                    self.find_click(self.__IS_NO_FOR_SALE)
            self.wait(self.__NUMBER)
            self.images_log(img_name, "输入信息")
        return ProductListingPage(self.driver)

    def put_away(self, img_name=None):     # 商品上架
        self.find_click(self.__PUT_AWAY)
        if self.find(self.__PUT_AWAY_ERROR):  # 上架报错
            text = self.find_text(self.__PUT_AWAY_ERROR)
            self.images_log(img_name, "上架报错")
            return text
        from .page_jumps import PageJumps
        return PageJumps(self.driver)
